package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.DateFormatUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.poi.excel.Excel;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.extend.model.ExtendSysFile;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.NumUtils;
import com.ccp.dev.qbdms.util.ProjectUUIDUtils;
import com.ccp.dev.qbdms.util.ReadAttachmentUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.ccp.dev.qbdms.dao.WQbdProblemHandleDao;
import com.ccp.dev.qbdms.model.WQbdProblemHandle;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 现场问题处理单 信息操作服务类
 *
 * @author prh
 * @date 2020-10-20 14:39:40
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WQbdProblemHandleService extends BaseService<WQbdProblemHandle> {

    @Resource
    private WQbdProblemHandleDao dao;
    @Resource
    private CommonalityService commonalityService;

    public HSSFWorkbook exportExcel(Excel excel,QueryFilter queryFilter){
        List<WQbdProblemHandle> dataList = this.dao.queryAll(queryFilter);
        int startRow = 1;
        //给每一个单元格赋值
        for (WQbdProblemHandle data : dataList) {
        excel.cell(startRow, 0).value(data.getId());
        //excel.cell(startRow, 1).value(data.getMc());
        //可在赋值时，根据需求进行格式化
        //excel.cell(startRow, 2).value(DateFormatUtil.formatDate(data.getRq()));
        //excel.cell(startRow, 3).value(data.getSj());
        //excel.cell(startRow, 4).value(data.getTenantId());
        startRow++;
        }
        return excel.getWorkBook();
    }

    /**
    * 查询上报的业务数据
    *
    * @param
    * @return
    */
    public List<WQbdProblemHandle> reportList(String dataId, String tableName) {
        return dao.reportList(dataId, tableName);
    }

    /**
    * 导出用查询
    *
    * @param queryFilter 查询参数
    * @return list
    */
    public List<WQbdProblemHandle> queryAll(QueryFilter queryFilter) {
        return dao.queryAll(queryFilter);
    }

    /**
    * 导入新增
    *
    * @param list 数据集
    * @throws Exception 异常
    */
    public void addImportData(List<WQbdProblemHandle> list) {
        ZhiLiangConst.addCommonFields(list);
        //上报状态
        BaseUtil.SystemLevel currentSystemLevel = BaseUtil.getCurrentSystemLevel();
        list.forEach(a -> {
            String mainId = ProjectUUIDUtils.getUUIDFor32();
            a.setId(mainId);
            //上报状态
            if (BaseUtil.SystemLevel.HOSPITALLEVEL.equals(currentSystemLevel)) {
                a.setReportStatus(ZhiLiangConst.REPORT_STATUS_GROUPTOBEREPORTED);
            } else if (BaseUtil.SystemLevel.PLANTLEVEL.equals(currentSystemLevel)) {
                a.setReportStatus(ZhiLiangConst.getDataInitStatusToHospital());
            }
        });
        if (!list.isEmpty()) {
            dao.addList(list);
        }
    }


    /**
    * 保存接口传输的数据
    *
    * @param wQbdProblemHandleList 主表model
    * @return
    */
    public void saveAll(List<WQbdProblemHandle> wQbdProblemHandleList, List<ExtendSysFile> extendSysFileList) {
        ReadAttachmentUtil.getEntityData(wQbdProblemHandleList, extendSysFileList);
            wQbdProblemHandleList.forEach(wQbdProblemHandle -> {
            wQbdProblemHandle.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath(wQbdProblemHandle.getFilldeptid()));
            if (BeanUtils.isEmpty(dao.getById(wQbdProblemHandle.getId()))) {
                dao.add(wQbdProblemHandle);
            } else {
                dao.update(wQbdProblemHandle);
            }
        });
    }

    /**
     * 获取新的注册编号
     * @return
     */
    public String generateNewRegisternum() {
        String maxRegisternum = dao.getMaxRegisternum();
        String newRegisternum="";
        String nowYearMonth = DateFormatUtil.getNowByString("yyyy");
        if (StringUtil.isNotEmpty(maxRegisternum)) {
            String pre = maxRegisternum.substring(0,Math.min(maxRegisternum.length(),4));
            if (!pre.equals(nowYearMonth)) {
                newRegisternum = nowYearMonth + "001";
            } else {
                String max = maxRegisternum.substring(4,maxRegisternum.length());
                newRegisternum = NumUtils.getNum(maxRegisternum, max);
            }
        } else {
            newRegisternum = nowYearMonth + "001";
        }
        return newRegisternum;
    }

    /**
     * 获取新的注册编号
     * @return
     */
    public String generateNewRegisternumS(String numb, String room) {
        String newRegisternum=room + "CLD-" + numb.substring(0,4) + numb.substring(4,numb.length());
        return newRegisternum;
    }
}
